package com.arvato.wxwork.service.member;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.common.config_bean.WxworkConfig;
import com.arvato.common.config_bean.ZSTBrandNumberConfig;
import com.arvato.common.https.HttpUtil;
import com.arvato.common.util.Dom4jUtils;
import com.arvato.common.util.JsonUtil;
import com.arvato.common.util.MD5Util;
import com.arvato.common.util.XStreamUtil;
import com.arvato.common.webservice.pos.WebServiceUtils;
import com.arvato.wxwork.bean.bean_dto.WxworkCustomerDto;

/**
 * 资生堂会员service
 * @author zhan655
 *
 */
@Service
@Transactional
public class WxworkMemberInfoService {
	
	private Logger logger = LoggerFactory.getLogger(WxworkMemberInfoService.class);
	
	@Autowired
	private JsonUtil jsonUtil;
	
	@Autowired
	private WebServiceUtils webserviceUtils;

	/**
	 * 得到资生堂会员的基本信息
	 * 调用接口相关：token生成规则为方法名+“#ad2*8jfdsas(^@”，进行MD5加密
	 * @param unionid
	 * @param brand
	 */
	public Map<String, Object> getMemberBaseInfo(String unionid, String brand) {
		// 根据brand 获得brandcode
		String brandCode = ZSTBrandNumberConfig.BRAND_CODE_MAP.get(brand);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("CustomerNumber", unionid);
		paramMap.put("OwnerId", brandCode);
		String url=WxworkConfig.QueryCustomerBaseInfo;
		String methodName="QueryCustomerBaseInfo";
		return sendRequest(url,methodName, paramMap);
	}
	
	/**
	 * 会员的基本信息
	 * @param customerNumber
	 * @param brand 
	 * @return
	 */
	public Map<String, Object> getMemberInfo(String customerNumber, String brand) {
		try {
			WxworkCustomerDto dto = new WxworkCustomerDto();
			String ownerId = ZSTBrandNumberConfig.BRAND_CODE_MAP.get(brand);
			String storeId = ZSTBrandNumberConfig.BRAND_STORE_MAP.get(ownerId);
			dto.setMemberNo(customerNumber);
			dto.setOwnerId(ownerId);
			dto.setStoreId(storeId);
			dto.setName("EC_MemberInfoQuery");
			String strXml = webserviceUtils.PostInterFace(dto.getOwnerId(), dto.getName(), XStreamUtil.createXml(dto));
			String jsonStr=Dom4jUtils.xml2jsonObjesct(strXml).toString();	
			logger.info("请求会员信息参数:" + dto.toString() + "返回值:" + jsonStr);
			Map<String, Object> infoMap = jsonUtil.str2Map(jsonStr);
			return infoMap;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return null;
	}
	
	/**
	 * 得到资生堂会员的订单列表
	 * 调用接口相关：token生成规则为方法名+“#ad2*8jfdsas(^@”，进行MD5加密
	 * @param customerNumber 用户在品牌方的会员号
	 */
	public Map<String, Object> getMemberOrderIist(String customerNumber) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("CustomerNumber", customerNumber);
		String url=WxworkConfig.QueryCustomerOrders;
		String methodName="QueryCustomerOrders";
		return sendRequest(url,methodName, paramMap);
	}
	
	/**
	 * 获取一条订单详情信息
	 * @param orderId
	 * @return
	 */
	public Map<String, Object> getMemberOrderDetail(String orderId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderid", orderId);
		String methodName="QueryCustomerOrderDetails";
		String url=WxworkConfig.QueryCustomerOrderDetails;
		return sendRequest(url, methodName,paramMap);
	}
	
	/**
	 * 获取会员当前积分
	 * @param customerNumber
	 * @return
	 */
	public Map<String, Object> getMemberPoint(String customerNumber, String brand) {
		try {
			WxworkCustomerDto dto = new WxworkCustomerDto();
			String ownerId = ZSTBrandNumberConfig.BRAND_CODE_MAP.get(brand);
			String storeId = ZSTBrandNumberConfig.BRAND_STORE_MAP.get(ownerId);
			dto.setOwnerId(ownerId);
			dto.setStoreId(storeId);
			dto.setMemberNo(customerNumber);
			dto.setName("EC_MemberPointQuery");
			String strXml = webserviceUtils.PostInterFace(dto.getOwnerId(), dto.getName(), XStreamUtil.createXml(dto));
			String jsonStr = Dom4jUtils.xml2jsonObjesct(strXml).toString();
			logger.info("请求会员积分参数:" + dto.toString() + "返回值:" + jsonStr);
			Map<String, Object> pointMap = jsonUtil.str2Map(jsonStr);
			return pointMap;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return null;
	}
	
	/**
	 * 获取用户积分历史
	 * @param customerNumber
	 * @return
	 */
	public Map<String, Object> getMemberPointHistory(String customerNumber) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("CustomerNumber", customerNumber);
		String methodName="QueryCustomerPointHistory";
		String url=WxworkConfig.QueryCustomerPointHistory;
		return sendRequest(url,methodName, paramMap);
	}
	
	/**
	 * 获取用户领用礼品历史
	 * @param customerNumber
	 * @return
	 */
	public Map<String, Object> getMemberGifts(String customerNumber) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("CustomerNumber", customerNumber);
		String methodName="QueryCustomerGifts";
		String url=WxworkConfig.QueryCustomerGifts;
		return sendRequest(url,methodName, paramMap);
	}
	
	/**
	 * 获取用户偏好爱好
	 * @param customerNumber
	 * @return
	 */
	public Map<String, Object> getMemberHabits(String customerNumber) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("CustomerNumber", customerNumber);
		String methodName="QueryCustomerHabits";
		String url=WxworkConfig.QueryCustomerHabits;
		return sendRequest(url,methodName, paramMap);
	}
	
	/**
	 * 有很多会员信息需要从外部接口获取(我们的系统只是资生堂的一小部分)
	 * 该方法统一发送请求，返回数据
	 * @param methodName 请求方法
	 * @param paramMap 请求参数
	 * @return
	 */
	private Map<String, Object> sendRequest(String url,String methodName, Map<String, Object> paramMap){
		String token = MD5Util.md5(methodName + "#ad2*8jfdsas(^@");
		paramMap.put("token", token);
		logger.info(methodName + ":" + url + "请求参数:" + jsonUtil.obj2String(paramMap));
		long startTime = System.currentTimeMillis();
		String responseStr = HttpUtil.doPostJsonData(url,paramMap);
		long endTime = System.currentTimeMillis();
		logger.info(methodName + "时间:" + (endTime - startTime));
		logger.info(methodName+ "请求结果:" + responseStr);
		Map<String, Object> resultMap = jsonUtil.str2Map(responseStr);
		return resultMap;
	}

}
